package com.yonyou.crm.sys.modules.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.sys.modules.entity.FieldVO;
import com.yonyou.crm.sys.modules.entity.ItemLayoutItemVO;
import com.yonyou.crm.sys.modules.entity.LayoutVO;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

/**
 *  明细布局
 */
@Service
class ItemLayoutBizServiceImpl extends BaseBizServiceImpl<ItemLayoutItemVO>{
	@Autowired
	private FieldBizServiceImpl fieldBizServiceImpl;
	
	@Autowired 
	private ModuleBizServiceImpl moduleBizServiceImpl;
	
	public List<ItemLayoutItemVO> getItemLayout(Long layoutId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("layoutId", layoutId);
		List<ItemLayoutItemVO> itemLayoutItemList = new ArrayList<ItemLayoutItemVO>();
		itemLayoutItemList = dao.selectVoList(ItemLayoutItemVO.class, "ListItemLayoutItem", paramMap);
		Boolean isCustom = false;
		for (ItemLayoutItemVO itemLayoutItemVO:itemLayoutItemList) {
			if (itemLayoutItemVO.getTenantId() !=0) {
				isCustom = true;
				break;
			}
		}
		Iterator<ItemLayoutItemVO> it = itemLayoutItemList.iterator();
		while(it.hasNext()) {
			ItemLayoutItemVO itemLayoutItemVO = it.next();
			if (itemLayoutItemVO.getTenantId() == 0 && isCustom) {
				it.remove();
			} else {
				if (itemLayoutItemVO.getIsBlank() == 0)  {
					FieldVO field = fieldBizServiceImpl.getField(itemLayoutItemVO.getActualFieldFullname());
					itemLayoutItemVO.setActualField(field);		
				}
			}
		}
		return itemLayoutItemList;
	}
	
	public void saveItemLayout(LayoutVO layoutVO,ModuleVO  itemObj) {
		List<ItemLayoutItemVO> itemLayoutItemVOs = layoutVO.getItemLayout();
		Long layoutId = layoutVO.getId();
		for (int i =0; i< itemLayoutItemVOs.size(); i++) {
			ItemLayoutItemVO itemLayoutItemVO =  itemLayoutItemVOs.get(i);
			itemLayoutItemVO.setItemObjId(itemObj.getId());
			itemLayoutItemVO.setLayoutId(layoutId);
			String apiName = itemLayoutItemVO.getApiName();
			itemLayoutItemVO.setApiName(apiName);
			if (itemLayoutItemVO.getIsBlank() ==0)  {
				FieldVO field = fieldBizServiceImpl.getField(itemObj.getId(),apiName);
				itemLayoutItemVO.setActualFieldFullname(field.getFullname());
			}
		}
		addItems( itemLayoutItemVOs);
	}
	
	/**
	 * 删除明细布局及其所有字段
	 * 
	 * @return
	 */
	public int deleteItemLayout(Long layoutId, Long itemObjId) {
		return deleteItems(layoutId, itemObjId);
	}
	
	/**
	 * 删除明细布局所有字段
	 * 
	 * @return
	 */
	public int deleteItems(Long layoutId, Long itemObjId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("layoutId", layoutId);
		paramMap.put("itemObjId", itemObjId);
		return dao.delete(ItemLayoutItemVO.class, "deleteAllItems", paramMap);
	}
		
	public int addItems(List<ItemLayoutItemVO> itemLayoutItemVOs) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
		Long userId = Long.parseLong(userIdStr);
		Date now = new Date();
		for (int i = 0; i < itemLayoutItemVOs.size(); i++) {
			ItemLayoutItemVO itemLayoutItemVO = itemLayoutItemVOs.get(i);
			itemLayoutItemVO.setTenantId(tenantId);
			itemLayoutItemVO.setSysCreatedTime(now);
			itemLayoutItemVO.setSysModifiedTime(now);
			itemLayoutItemVO.setSysCreatedUserId(userId);
			itemLayoutItemVO.setSysModifiedUserId(userId);
		}
		return dao.saveSql(ItemLayoutItemVO.class, "batchInsertItems",
				itemLayoutItemVOs);
	}

	public List<ItemLayoutItemVO> getItems(Long itemObjId) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		List<ItemLayoutItemVO> itemLayoutItemVOs = new ArrayList<ItemLayoutItemVO>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tenantId", tenantId);
		paramMap.put("itemObjId", itemObjId);
		itemLayoutItemVOs = dao.selectVoList(ItemLayoutItemVO.class,
				"ListItemLayoutItem", paramMap);
		Boolean isCustom = false;
		for (ItemLayoutItemVO itemLayoutItemVO : itemLayoutItemVOs) {
			if (itemLayoutItemVO.getTenantId() != 0) {
				isCustom = true;
				break;
			}
		}
		Iterator<ItemLayoutItemVO> it = itemLayoutItemVOs.iterator();
		while (it.hasNext()) {
			ItemLayoutItemVO itemLayoutItemVO = it.next();
			if (itemLayoutItemVO.getTenantId() == 0 && isCustom) {
				it.remove();
			} else {
				if (itemLayoutItemVO.getIsBlank() == 0) {
					FieldVO fieldVO = fieldBizServiceImpl
							.getField(itemLayoutItemVO.getActualFieldFullname());
					itemLayoutItemVO.setActualField(fieldVO);
				}
			}
		}
		return itemLayoutItemVOs;
	}
		
}



